package com.qingxin.wuye.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.entity.WyCity;

@Service
public class WyCityService extends BaseService<WyCity, String> {

	
	@SuppressWarnings("unchecked")
	public List<JsTreeNode> getUITreeData(String companyid, LoginUser loginUser, boolean isParent){
		List<JsTreeNode> list = new ArrayList<>();
		if(loginUser == null){
			return list;
		}
		if(!SystemCfg.isAdminUser(loginUser)){
			companyid = loginUser.getCompanyid();
		}
		
		StringBuilder hql = new StringBuilder();
		hql.append("from WyCity p where 1=1 ");
		if(StringUtils.isNotEmpty(companyid) && !"0".equals(companyid)){
			hql.append(" and p.companyid='"+companyid.replace("'", "")+"' ");
		}
		hql.append(" order by p.cityName asc");
		List<WyCity> cities = listByHql(hql.toString());
		if(cities != null){
			for(WyCity city : cities){
				list.add(new JsTreeNode(city.getCityid(), city.getCityName(), SystemCfg.UI_DATATYPE_CITY, "cityid", isParent));
			}
		}
		return list;
	}
	
	
	public Pager list(String companyid, PageParam pageParam, HttpServletRequest request){
		Querier querier = Querier.create("from WyCity c where c.companyid=:companyid ");
		querier.setParam("companyid", companyid);
		
		querier.append(pageParam.getOrderby(" order by c.cityName asc"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 添加
	 * @param city
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(WyCity city, LoginUser loginUser, HttpServletRequest request){
		if(isNameDuplicate(city.getCityid(), null, city.getCompanyid())){
			return new SysError("名称已存在.");
		}
		
		Date now = new Date();
		city.setCreateTime(now);
		city.setUpdateTime(now);
		city.setCreateBy(loginUser.getLogin_name());
		city.setUpdateBy(loginUser.getLogin_name());
		
		this.save(city);
		return null;
	}
	
	
	/**
	 * 修改
	 * @param city
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(WyCity city, LoginUser loginUser, HttpServletRequest request){
		WyCity old = getSafeEntity(city.getCityid());
		if(old == null){
			return new SysError("信息不存在");
		}
		
		if(isNameDuplicate(city.getCityName(), old.getCityid(), old.getCompanyid())){
			return new SysError("名称已存在.");
		}
		old.setCityName(city.getCityName());
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		
		Updater<WyCity> updater = new Updater<>(old, "cityName,update_by,update_time");
		this.update(updater);
		
		return null;
		
	}
	
	
	/**
	 * 删除
	 * @param cityids
	 * @param request
	 * @return
	 */
	public SysError delete(String[] cityids, HttpServletRequest request){
		for(String cityid : cityids){
			WyCity city = get(cityid);
			if(city == null){
				continue;
			}
			
			this.delete(city);
		}
		return null;
	}
	
	
	
	/**
	 * 判断指定名称是否被使用
	 * @param cityName
	 * @param cityid
	 * @param companyid
	 * @return
	 */
	public boolean isNameDuplicate(String cityName, String cityid, String companyid){
		if(StringUtils.isEmpty(cityid)){
			Object val = getByHql("select cityid from WyCity where cityName=? and companyid=?", cityName, companyid);
			return val!=null;
		}else{
			Object val = getByHql("select cityid from WyCity where cityName=? and companyid=? and cityid<>?", cityName, companyid, cityid);
			return val!=null;
		}
	}
	
	
	
}
